package cn.com.edu.nju.ecrec.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;

import cn.com.edu.nju.ecrec.dao.ItemDao;
import cn.com.edu.nju.ecrec.model.Item;
import cn.com.edu.nju.ecrec.util.JPAFactory;

public class ItemDaoImpl implements ItemDao {

	private EntityManager em;

	public ItemDaoImpl() {
		em = JPAFactory.getManager();
	}

	public void finalize() {
		em.close();
	}

	@Override
	public void commit() {
		em.getTransaction().commit();
	}

	@Override
	public void begin() {
		if (em == null)
			em = JPAFactory.getManager();
		if (!em.isOpen())
			em = JPAFactory.getManager();
		if (!em.getTransaction().isActive())
			em.getTransaction().begin();
	}

	@Override
	public void deleteById(long id) {
		this.begin();
		Item i = em.find(Item.class, id);
		em.remove(i);
		this.commit();
	}

	@Override
	public List<Long> getIds() {
		this.begin();
		String sql = "select i.pid from Item i";
		List<Long> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public int getCount() {
		this.begin();
		String sql = "select count(i) from Item i";
		int count = Integer.parseInt(em.createQuery(sql).getResultList().get(0).toString());
		return count;
	}

	@Override
	public List<Item> getAll() {
		this.begin();
		String sql = "select i from Item i";
		List<Item> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public Item getByUriName(String uri_name) {
		this.begin();
		String sql = "SELECT i FROM Item i where i.uri_name='"+uri_name+"'";
		List<Item> list = em.createQuery(sql).getResultList();
		if(!list.isEmpty())
			return list.get(0);
		else
			return null;
	}

	@Override
	public List<Item> getByBrand(String brand) {
		this.begin();
		String sql = "SELECT i FROM Item i where i.brand='" + brand
				+ "'";
		List<Item> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<Item> getByCategory(String category) {
		this.begin();
		String sql = "SELECT i FROM Item i where i.category='" + category
				+ "'";
		List<Item> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<Item> getByClassify(String classify) {
		this.begin();
		String sql = "SELECT i FROM Item i where i.classify='" + classify
				+ "'";
		List<Item> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<Item> getByGoodsPrice(double goods_price) {
		this.begin();
		String sql = "SELECT i FROM Item i where i.goods_price=" + goods_price;
		List<Item> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<Item> getByColor(String color) {
		this.begin();
		String sql = "SELECT i FROM Item i where i.color='" + color
				+ "'";
		List<Item> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getBrands() {
		this.begin();
		String sql = "SELECT distinct i.brand FROM Item i";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getCategories() {
		this.begin();
		String sql = "SELECT distinct i.category FROM Item i";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getClassifies() {
		this.begin();
		String sql = "SELECT distinct i.classify FROM Item i";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<Double> getGoodsPrices() {
		this.begin();
		String sql = "SELECT distinct i.goods_price FROM Item i";
		List<Double> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getColors() {
		this.begin();
		String sql = "SELECT distinct i.color FROM Item i";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getUriNames() {
		this.begin();
		String sql = "SELECT distinct i.uri_name FROM Item i";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

}
